//
//  LXCarBlePasswordViewController.m
//  INMOTION_CustomMadeProject
//
//  Created by cjg on 2021/3/25.
//  Copyright © 2021 cjg. All rights reserved.
//

#import "LXCarBlePasswordViewController.h"
#import "L9_CarModel.h"
#import "KSLanguageManager.h"
#import "NSObject+HUD.h"
#import "IMTConfig.h"
#import "IMFunction.h"

@interface LXCarBlePasswordViewController ()

@end

@implementation LXCarBlePasswordViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = IMTNewCarLocalizedString(@"activity_change_bt_pwd_title");
    [self.changeBtn setTitle:IMTNewCarLocalizedString(@"activity_change_bt_pwd_btn_text") forState:(UIControlStateNormal)];
    self.oldPasswordTF.placeholder = IMTNewCarLocalizedString(@"activity_change_bt_pwd_hint_old_pwd");
    self.nowPasswordTF.placeholder = IMTNewCarLocalizedString(@"activity_change_bt_pwd_hint_new_pwd");
    self.surePasswordTF.placeholder = IMTNewCarLocalizedString(@"activity_change_bt_pwd_hint_confirm_new_pwd");

    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back_white"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = left;

    [self.carModel readBlePasswordCompleBlock:^(NSError *error, NSData *responseData) {
        
    }];
}

// 修改密码
- (IBAction)changePasswordAction:(UIButton *)sender {
    if ([self checkTextFieldInput]) {
        // 验证旧密码与当前车内密码是否一致
        [self checkOldPassword];
    }
}

// 输入判断
- (BOOL)checkTextFieldInput {
    if (self.nowPasswordTF.text.length != 6 || self.oldPasswordTF.text.length != 6 || self.surePasswordTF.text.length != 6) {
        [self showErrorHUDWithText:IMTNewCarLocalizedString(@"prompt_change_bt_pwd_error_old_pwd")];
        return NO;
    }
    if (![self.nowPasswordTF.text isEqualToString:self.surePasswordTF.text]) {
        [self showErrorHUDWithText:IMTNewCarLocalizedString(@"prompt_change_bt_pwd_confirm_pwd_not_match")];
        return NO;
    }
    return YES;
}

// 验证旧密码是否正确
- (void)checkOldPassword {
    NSData *param = [IMFunction strToAscll:self.oldPasswordTF.text];
    [self.carModel checkBlePasswordWithPassword:param successBlock:^{
        [self safeConnect];
    } failBlock:^(NSError *error) {
        [self showErrorHUDWithText:IMTNewCarLocalizedString(@"prompt_change_bt_pwd_error_old_pwd")];
    }];
}

// 安全连接
- (void)safeConnect {
    WS(weakSelf)
    [self.carModel getSafeChannelWithCompleBlock:^(NSError *error, NSData *responseData) {
        if (error == nil && weakSelf.carModel.randomCode) {
            [weakSelf.carModel setSafePasswordWithPassword:weakSelf.carModel.randomCode successBlock:^{
                [weakSelf writePasswordToCar];
            } failBlock:^(NSError *error) {
                [weakSelf showErrorHUDWithText:IMTNewCarLocalizedString(@"setting_failed")];
                return;
            }];
        }else {
            [weakSelf showErrorHUDWithText:IMTNewCarLocalizedString(@"setting_failed")];
            return;
        }
    }];
}

// 写入密码
- (void)writePasswordToCar {
    NSData *param = [IMFunction strToAscll:self.nowPasswordTF.text];
    [self.carModel writeBlePasswordWithPassword:param successBlock:^{
        self.carModel.blePassword = self.nowPasswordTF.text;
        [self.carModel saveCarBlePassword];
        [self showSuccessHUDWithText:IMTNewCarLocalizedString(@"setting_success")];
        [self.navigationController popViewControllerAnimated:YES];
    } failBlock:^(NSError *error) {
        [self showErrorHUDWithText:IMTNewCarLocalizedString(@"setting_failed")];
    }];
}

#pragma mark - TextFieldDeleagte
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.location >= 6) {
        return NO;
    }
    return YES;
}


@end
